using System;
using System.Collections.Generic;
using System.Text;
using Mpi.Common.Collections;
using System.ComponentModel;
using System.Diagnostics;
using Mpi.Common;

namespace Xeml.Document
{
    /// <summary>
    /// Represents an observation point within a story.
    /// </summary>
    public class ObservationPoint : XemlAnnotatableObject, INotifyPropertyChanged, ITimeObject
    {
        #region Fields
        private int _id;
        private TimeSpan _timePoint;
        private ObservationCollection _observations;

        #endregion

        #region Properties

        /// <summary>
        /// The collection of observations are made at this point.
        /// </summary>
        public ObservationCollection Observations
        {
            get { return _observations; }
        }

        /// <summary>
        /// Defines the duration of the observation.
        /// </summary>
        public TimeSpan Duration
        {
            get
            {
                TimeSpan ret = TimeSpan.Zero;
                foreach (Observation o in this._observations)
                {
                    ret += o.Duration;
                }
                return ret;

            }

        }

        /// <summary>
        /// An unique identifiere within the storyboard.
        /// </summary>
        public int Id
        {
            get
            {
                return this._id;
            }
            set
            {
                if (this._id != value)
                {
                    this._id = value;
                    OnPropertyChanged("Id");
                }
            }
        }


        /// <summary>
        /// The timepoint of this observation point.
        /// </summary>
        public TimeSpan TimePoint
        {
            get
            {
                return this._timePoint;
            }
            set
            {
                TimeSpan oldVal = this._timePoint;
                this._timePoint = value;
                Debug.WriteLine("ObservationPoint: TimpointChanged 1 ");
                if (oldVal != this._timePoint)
                {
                    Debug.WriteLine("ObservationPoint: TimpointChanged old=" + oldVal.ToString() + " new=" + _timePoint.ToString());
                    OnTimePointChanged(oldVal, this._timePoint);
                    OnPropertyChanged("Time");
                }
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// The standard constructor.
        /// </summary>
        public ObservationPoint()
        {
            this._observations = new ObservationCollection(this);
            this._observations.CollectionChanged += new EventHandler(OnObservationsChanged);
        }

        #endregion

        #region Methods

        private void OnObservationsChanged(object sender, EventArgs e)
        {
            OnPropertyChanged("Observations");
        }

        /// <summary>
        /// Converts this object to its string representation
        /// </summary>
        /// <returns>The string representaion of this object.</returns>
        public override string ToString()
        {
            return this._id + "@" + this._timePoint.ToString();
        }

        #endregion

            
        private ObservationPointCollection _collection;
        public ObservationPointCollection Collection
        {
            get
            {
                return this._collection;
            }
            internal set
            {
                if (value != _collection )
                {
                    this._collection = value;
                }


            }
        }

     

        #region Event-/handling
        //  public event EventHandler Changed;
        public event EventHandler<PropertyHasChangedEventArgs<TimeSpan>> TimePointHasChanged;

    

        protected virtual void OnTimePointChanged(TimeSpan oldVal, TimeSpan newVal)
        {
            if (TimePointHasChanged != null)
                TimePointHasChanged(this, new PropertyHasChangedEventArgs<TimeSpan>(oldVal, newVal));
        }
        #endregion

        #region INotifyPropertyChanged Members

        protected virtual void OnPropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
